<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Laravel\Sanctum\HasApiTokens;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Support\Facades\Storage;

class Users extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable;
    static $unguarded = true;
    protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims(): array
    {
        return [];
    }

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
//        $status_show =  getStatusValue($val['status']);
        $image_path = 'https://alumnus.biaoxufilm.com/storage/file/20231129210449.png';
        $image_show = '';
        if($val['avatar']){
            $image_path = getImageUrl($val['avatar']);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }
        return [
            'id'=>$val['id'],
            'openid'=>$val['openid'],
            'username'=>$val['username'],
            'avatar'=>$image_path,
            'avatar_show'=>$image_show,
            'name'=>$val['name'],
            'mobile'=>$val['mobile'],
            'email'=>$val['email'],
            'degree'=>$val['degree'],
            'school_time'=>$val['school_time'],
            'college'=>$val['college'],
            'major'=>$val['major'],
            'school_level'=>$val['school_level'],
            'habitation'=>$val['habitation'],
            'work_location'=>$val['work_location'],
            'workplace'=>$val['workplace'],
            'profession'=>$val['profession'],
            'post'=>$val['post'],
            'is_alumni'=>$val['is_alumni'],
//            'status_show'=>$status_show,
//            'status_html_show'=>$status_html_show,
            'created_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
        ];
    }


    /**
     * @param $where
     * @param $offset
     * @param $limit
     * @param string $orderby
     * @return mixed
     * 判断条件获取列表数据
     */
    public function  getList($where,$offset,$limit,$orderby='')
    {

        $user = auth('api')->user();
        $data = self::query();

        $data =   $data ->where($where)->where('id','<>',$user['id']);
        if($orderby  == 'created_at'){
            $data->latest($orderby);
        }
        if($orderby  == 'id'){
            $data->oldest($orderby);
        }else{
            $data->latest('id');
        }

        $vals =   $data->offset($offset)->limit($limit)
            ->get();

        return $vals;
    }
}
